package com.degon.hsyl.customchatapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.degon.hsyl.customchatapp.R;
import com.degon.hsyl.customchatapp.bean.Linkman;
import com.degon.hsyl.customchatapp.ui.SendMessageActivity;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

public class LinkmanListAdapter extends RecyclerView.Adapter<LinkmanListAdapter.ViewHolder> {

    private List<Linkman> linkmen;
    private Context context;
    private String senderId;

    public LinkmanListAdapter(List<Linkman> list, Context context, String senderId) {
        this.linkmen = list;
        this.context = context;
        this.senderId = senderId;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_list_item_view, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Linkman linkman = linkmen.get(position);
        holder.headPic.setImageURI(Uri.parse(linkman.getHeadPortrait()));
        holder.userName.setText(linkman.getName());

        holder.mainView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SendMessageActivity.class);
                intent.putExtra("targetId", linkman.getUserId());
                intent.putExtra("senderId", senderId);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return linkmen.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        SimpleDraweeView headPic;
        TextView userName;
        RelativeLayout mainView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            headPic = itemView.findViewById(R.id.head_pic);
            userName = itemView.findViewById(R.id.list_user_name);
            mainView = itemView.findViewById(R.id.main_view);
        }
    }

}
